# coding=utf-8
import os
from PIL import Image, ImageFont, ImageDraw


def imageOverlay():
    im = Image.open('face.jpg')
    draw = ImageDraw.Draw(im)
    fontsize = min(im.size) / 4
    font = ImageFont.truetype("khmerOS.ttf", 28)
    draw.text(((im.size[0] - fontsize-10), -10), '18', font=font, fill=(256, 0, 256))
    im.save('../dest/output.jpg', 'jpeg')


def processImage(filesource, destsource, name, imgtype):
    imgtype = 'jpeg' if imgtype == '.jpg' else 'png'
    im = Image.open(filesource + name)
    rate = max(im.size[0] / 640.0 if im.size[0] > 640 else 0, im.size[1] / 1136.0 if im.size[1] > 1136 else 0)
    if rate:
        im.thumbnail((im.size[0] / rate, im.size[1] / rate))
    im.save(destsource + name, imgtype)


def run():
    # 切换到当前目录
    os.chdir(os.getcwd())
    # 遍历当前目录中的文件
    for i in os.listdir(os.getcwd()):
        postfix = os.path.splitext(i)[1]
        if postfix == '.jpg' or postfix == '.png':
            processImage('./', '../dest/', i, postfix)

if __name__ == '__main__':
    #imageOverlay()
    run()
